//========================================================================
// *作者：海盗猫  主页：ilplay.com
// *脚本：HDMEditorSetting
// *描述：
// *?2021 MYJL . All rights reserved.
//* ======================================================================
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
namespace HDMCode.FrameEditor
{
    [Serializable]
    public class EditorDownloadPackedInfo
    {
        /// <summary>
        /// 包体的显示名称
        /// </summary>
        public string DisplayName = "";

        /// <summary>
        /// 包名
        /// </summary>
        public string packageName = "";

        /// <summary>
        /// 是否安装
        /// </summary>
        [HideInInspector]
        public bool isInstall = false;

        /// <summary>
        /// 包体的下载地址
        /// </summary>
        public string DownURL = "";

        /// <summary>
        /// 包体名称
        /// </summary>
        private string packagePath => Path.GetFullPath($"Packages/{packageName}");


        /// <summary>
        /// 获取包体的Editor程序
        /// </summary>
        /// <returns> 返回程序集 </returns>
        public Assembly GetEditorAssembly() 
        {
            Assembly assembly = null;
            foreach (var filesPath in Directory.GetFiles(packagePath+"/Editor"))
            {
                if (Path.GetExtension(filesPath) == ".asmdef")
                {
                    string assemblyPath = $"Packages/{packageName}/Editor/{Path.GetFileName(filesPath)}";
                    var assemblyFile = AssetDatabase.LoadAssetAtPath<AssemblyDefinitionAsset>(assemblyPath);
                    if (assemblyFile != null)
                    {
                        try
                        {
                            assembly = Assembly.Load(assemblyFile.name);
                        }
                        catch (Exception error)
                        {
                            Debug.LogError($"获取插件的程序集失败！{assemblyFile.name} {error}");
                        }
                    }
                }
            }
            return assembly;
        }
    }

    public class HDMEditorSetting : ScriptableObject
    {
        public List<EditorDownloadPackedInfo> downloadPackedInfo;
    }
}


